package com.neu.carbon.mes.controller;

import java.util.List;

import com.neu.carbon.mes.domain.MesProductModel;
import com.neu.carbon.mes.domain.MesProductPlan;
import com.neu.carbon.mes.domain.MesProductSchedule;
import com.neu.carbon.mes.service.IMesProductModelService;
import com.neu.carbon.mes.service.IMesProductPlanService;
import com.neu.carbon.mes.service.IMesProductScheduleService;
import com.neu.carbon.wms.domain.WmsMaterialInfo;
import com.neu.carbon.wms.service.IWmsMaterialInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.neu.common.annotation.Log;
import com.neu.common.core.controller.BaseController;
import com.neu.common.core.domain.AjaxResult;
import com.neu.common.enums.BusinessType;
import com.neu.carbon.mes.domain.MesProductJob;
import com.neu.carbon.mes.service.IMesProductJobService;
import com.neu.common.utils.poi.ExcelUtil;
import com.neu.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 作业装配Controller
 * 
 * @author neuedu
 * @date 2024-09-10
 */
@Api(tags = {"作业装配"})
@RestController
@RequestMapping("/mesProduct/productJob")
public class MesProductJobController extends BaseController
{
    @Autowired
    private IMesProductJobService mesProductJobService;
    @Resource
    private IMesProductPlanService productPlanService;
    @Resource
    private IMesProductScheduleService mesProductScheduleService;
    @Resource
    private IMesProductModelService mesProductModelService;


    @Resource
    private IWmsMaterialInfoService iWmsMaterialInfoService;


        /**
     * 查询作业装配列表
     */
    @GetMapping("/list")
    @ApiOperation("查询作业装配列表")
    @DynamicResponseParameters(properties = {
	        @DynamicParameter(name = "total", value = "总记录数"),
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "rows", value = "返回业务数据（数组类型）", dataTypeClass = MesProductJob.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    public TableDataInfo list(MesProductJob mesProductJob)
    {
        startPage();
        List<MesProductJob> list = mesProductJobService.selectMesProductJobList(mesProductJob);
        list.forEach(model->{
            MesProductPlan mesProductPlan = productPlanService.selectMesProductPlanById(model.getPlanId());
            if(mesProductPlan!=null){
                model.setPlanName(mesProductPlan.getSerialNo());
            }
            MesProductSchedule mesProductSchedule = mesProductScheduleService.selectMesProductScheduleById(model.getScheduleId());
            if (mesProductSchedule!=null){
                model.setScheduleName(mesProductSchedule.getSerialNo());
            }
            MesProductModel mesProductModel = mesProductModelService.selectMesProductModelById(model.getId());
            if (mesProductModel!=null){
                model.setProductName(mesProductModel.getModelName());
            }
            WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getProductId());
            if (wmsMaterialInfo!=null){
                model.setProductName(wmsMaterialInfo.getName());
                model.setModel(wmsMaterialInfo.getModel());
                model.setSpecification(wmsMaterialInfo.getSpecification());
                model.setUnit(wmsMaterialInfo.getUnit());

            }
        });
        return getDataTable(list);
    }

    /**
     * 导出作业装配列表
     */
    @ApiOperation("导出作业装配列表")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "文件名")
    })
    @PreAuthorize("@ss.hasPermi('mesProduct:productJob:export')")
    @Log(title = "作业装配", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MesProductJob mesProductJob)
    {
        List<MesProductJob> list = mesProductJobService.selectMesProductJobList(mesProductJob);
        ExcelUtil<MesProductJob> util = new ExcelUtil<MesProductJob>(MesProductJob.class);
        return util.exportExcel(list, "productJob");
    }

    /**
     * 获取作业装配详细信息
     */
    @ApiOperation("获取作业装配详细信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "data", value = "返回业务数据", dataTypeClass = MesProductJob.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        MesProductJob model = mesProductJobService.selectMesProductJobById(id);
        MesProductPlan mesProductPlan = productPlanService.selectMesProductPlanById(model.getPlanId());
        if(mesProductPlan!=null){
            model.setPlanName(mesProductPlan.getSerialNo());
        }
        MesProductSchedule mesProductSchedule = mesProductScheduleService.selectMesProductScheduleById(model.getScheduleId());
        if (mesProductSchedule!=null){
            model.setScheduleName(mesProductSchedule.getSerialNo());
        }
        MesProductModel mesProductModel = mesProductModelService.selectMesProductModelById(model.getId());
        if (mesProductModel!=null){
            model.setProductName(mesProductModel.getModelName());
        }
        WmsMaterialInfo wmsMaterialInfo = iWmsMaterialInfoService.selectWmsMaterialInfoById(model.getProductId());
        if (wmsMaterialInfo!=null){
            model.setProductName(wmsMaterialInfo.getName());

        }
        return AjaxResult.success(model);
    }

    /**
     * 新增作业装配
     */
    @ApiOperation("新增作业装配")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesProduct:productJob:add')")
    @Log(title = "作业装配", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProductJob mesProductJob)
    {
        return toAjax(mesProductJobService.insertMesProductJob(mesProductJob));
    }

    /**
     * 修改作业装配
     */
    @ApiOperation("修改作业装配")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesProduct:productJob:edit')")
    @Log(title = "作业装配", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProductJob mesProductJob)
    {
        return toAjax(mesProductJobService.updateMesProductJob(mesProductJob));
    }

    /**
     * 删除作业装配
     */
    @ApiOperation("删除作业装配")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasPermi('mesProduct:productJob:remove')")
    @Log(title = "作业装配", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesProductJobService.deleteMesProductJobByIds(ids));
    }
}
